import 'package:get/get.dart';

import '../../../models/user_statistics_entity.dart';
import '../../../services/api_service.dart';
import '../../../services/common_service.dart';

class ExpenseLogic extends GetxController {
  final apiService = Get.find<ApiService>();
  final commonService = Get.find<CommonService>();

  var isLoading = true.obs;
  var statisticsInfo = UserStatisticsEntity().obs;

  var year = Rxn<String>();
  var yearList = List<String>.generate(
      5, (index) => (DateTime.now().year - index).toString());

  var touchBarIndex = Rxn<int>();

  @override
  void onInit() {
    super.onInit();

    year.value = DateTime.now().year.toString();

    _getStatistics(year.value!);
  }

  void onYearChange(String value) {
    year.value = value;
    _getStatistics(value);
  }

  void _getStatistics(String year) {
    isLoading.value = true;

    String? role = commonService.getRole();

    apiService.statistics(year, role).then((value) {
      isLoading.value = false;
      if (value != null) {
        statisticsInfo.value = value;
      } else {
        Get.snackbar('提示', '获取统计数据失败');
      }
    });
  }
}
